<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Advise extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	/*
	 *返回建议列表页面
	 */
	public function index() {
		$this->load->library ( 'pagination' );
		// 每页显示数据条数
		$per_page=20;//暂定为20条
		
		$config['base_url']=site_url('advise/index');
		$this->load->model ( 'advise_model' );
		// 数据总数
		$config ['total_rows'] = $this->advise_model->count_advises();
		//$config ['total_rows']=200;
		$config ['per_page'] = $per_page;
		$config ['first_link'] = '第一页';
		$config ['last_link'] = '最后一页';
		$config ['next_link'] = '下一页';
		$config ['prev_link'] = '上一页';
		$config['uri_segment'] =3;
		
		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		//偏移量
		$offset=intval($this->uri->segment(3));
		$data ['advises'] = $this->advise_model->find_all_advises ($per_page,$offset);
		$data ['title'] = '建议管理 - 集市宝仓库管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'advise/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回回复建议页面
	 */
	public function reply() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'advise_model' );
		$data ['advise'] = $this->advise_model->load ( $id );
		$data ['title'] = '回复建议 - 集市宝仓库管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'advise/reply' );
		$this->load->view ( 'footer' );
	}
	/*
	 *保存建议信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$advise = array (
				'reply' => $this->input->post ( 'content' )
		);
		$this->load->model ( 'advise_model' );
		if (!empty($id)) {
			if($this->advise_model->update ( $id, $advise )){
				return_json(TRUE,'','回复成功！'); 
			}else{
				return_json(FALSE,'','回复失败！');
			}
		} 
	}
}